package com.kxq.zhuoyue.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kxq.zhuoyue.po.Dept;
import com.kxq.zhuoyue.service.DeptService;
import com.kxq.zhuoyue.po.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping("/findAll")
    public Result findAll(){
        List<Dept> list = deptService.list();
        return Result.success(list);
    }

    //分页查询所有
    @PostMapping("/findByPage")
    public Result findByPage(@RequestBody Dept dept){
        Page<Dept> page = new Page<>(dept.getPageNum(),dept.getPageSize());
        IPage<Dept> ipage = deptService.page(page);
        List<Dept> list = ipage.getRecords();
        return Result.success(list);
    }

    //分页条件查询
    @PostMapping("/findByNamePage")
    public Result findByNamePage(@RequestBody Dept dept){
        //创建QueryWrapper查询对象
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dname","测试部"); //查询条件 dname 等于 hello
        Page<Dept> page = new Page<>(dept.getPageNum(),dept.getPageSize());
        IPage<Dept> ipage = deptService.page(page,queryWrapper);
        return Result.success(ipage.getRecords());
    }

    //条件精确查询
    @PostMapping("/findByName")
    public Result findByName(@RequestBody Dept dept){
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("dname",dept.getDeptName()); //查询条件 dname 等于 hello
        List<Dept> list = deptService.list(queryWrapper);
        return Result.success(list);
    }

    //条件模糊查询
    @PostMapping("/findByNameLike")
    public Result findByNameLike(@RequestBody Dept dept){
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("dname",dept.getDeptName()); //查询条件 dname 等于 hello
        List<Dept> list = deptService.list(queryWrapper);
        return Result.success(list);
    }
}
